package com.bootcamp.ex1124.aop;

import com.bootcamp.ex1124.exception.ParameterException;
import com.bootcamp.ex1124.util.ValidationUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/**
 * @program: ex1124
 * @description:
 * @author: Keyu Li
 * @create: 2021-11-25 10:15
 **/

@Aspect
@Component
public class CustomAspect {
    @Pointcut("execution(* com..controller..*(..))")
    public void myPointcut(){}

    @Before("myPointcut()")
    public void before(JoinPoint join) throws ParameterException {
        Object[] args = join.getArgs();
        for (Object arg : args) {
            if (arg instanceof BindingResult){
                ValidationUtil.valid((BindingResult) arg);
            }
        }
    }
}
